package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.config.satoken.SaTokenUtilPy;
import com.ls.entity.BlackList;
import com.ls.entity.Customer;
import com.ls.enums.RoleEnum;
import com.ls.mapper.BlackListMapper;
import com.ls.service.IBlackListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.ls.constant.SystemConstants.TEL_NUMBER_REGEX;

/**
 * <p>
 * 电话黑名单 服务实现类
 * </p>
 *
 * @author pengyu
 * @since 2024-10-12
 */
@Service
@RequiredArgsConstructor
public class BlackListServiceImpl extends ServiceImpl<BlackListMapper, BlackList> implements IBlackListService {
    /** 新增黑名单 */
    @Override
    public ResultData add(String phone) {
        Customer customer = SaTokenUtilPy.getCustomer();
        RoleEnum roleEnum = RoleEnum.getRoleEnum(customer.getRoleId());
        if (RoleEnum.COMMON.equals(roleEnum)){
            return ResultData.noPermission("非会员不能新增黑名单！");

        }
        if (!phone.matches(TEL_NUMBER_REGEX)) {
            return ResultData.failure("手机号格式不正确");
        }
        if(Objects.equals(customer.getPhone(), phone)){
            return ResultData.failure("不能添加自己");
        }
        BlackList blackList;
        blackList = getOne(new LambdaQueryWrapper<BlackList>()
                .eq(BlackList::getCustomerId, customer.getCustomerId())
                .eq(BlackList::getBlackPhone, phone));
        if (blackList != null) {
            return ResultData.failure("手机号已存在");
        }
        blackList = new BlackList(phone, customer.getCustomerId(), customer.getPhone());
        blackList.insert();
        return ResultData.successMsg("新增成功");
    }
    /** 删除黑名单 */
    @Override
    public ResultData del(Long blackId) {
        BlackList blackList=getById(blackId);
        Customer customer = SaTokenUtilPy.getCustomer();
        if(!Objects.equals(blackList.getCustomerId(), customer.getCustomerId())){
            return ResultData.failure("黑名单不存在");
        }
        if (blackList==null){
            return ResultData.failure("黑名单不存在");
        }
        return blackList.deleteById()?ResultData.successMsg("删除成功"):ResultData.failure("删除失败");
    }
    /** 查询黑名单列表 */
    @Override
    public ResultData pageList(String phone,Integer pageNum,Integer pageSize) {
        Customer customer = SaTokenUtilPy.getCustomer();
        LambdaQueryWrapper<BlackList> wrapper = new LambdaQueryWrapper<BlackList>()
                .eq(BlackList::getCustomerId, customer.getCustomerId())
                .eq(StringUtils.isNotBlank(phone), BlackList::getBlackPhone, phone)
                .orderByDesc(BlackList::getCreateTime);
        Page page = new Page(pageNum, pageSize);
        IPage<BlackList> dtoIPage = this.baseMapper.selectPage(page, wrapper);
        return ResultData.successData(dtoIPage);
    }
}
